package cn.com.dhc.zzz.back.action;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.bbs.ba.base.BAAction;
import cn.bbs.ba.factory.BABeanFactory;
import cn.bbs.ba.format.ZZZFormat;
import cn.bbs.ba.html.ZPaiConstant;
import cn.bbs.ba.util.DtoUtil;
import cn.com.zzz.dao.PkJFFlowDao;
import cn.com.zzz.paypal.util.paypalfunctions;
import cn.com.zzz.pk.dto.PkJFFlow;

import com.paypal.sdk.core.nvp.NVPDecoder;
import com.paypal.sdk.core.nvp.NVPEncoder;
import com.paypal.sdk.services.NVPCallerServices;

/**
 * 处理金钱action
 */
public class PKMoneyAction extends BAAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		super.execute(mapping, form, request, response);
		PkJFFlow pkJFFlow = new PkJFFlow();
		DtoUtil.formToDto(form, pkJFFlow);
		if (super.userInfo == null) {
			return mapping.findForward("comError");
		}
		PkJFFlowDao userInfoDao = (PkJFFlowDao)BABeanFactory.getBean("JFFLOW");
		// 更新基本信息
		int insertCnt = 0;
		
		// 充值
		if ("chongzhi".equals(pkJFFlow.getUpdateFlag())) {
			// 流水号码
			pkJFFlow.setFlowNo(ZZZFormat.getSysteTime());
			// 不一定能成功。
			pkJFFlow.setEx1("0");
			// 时间
			pkJFFlow.setExDate(ZZZFormat.getTime());
			//
			pkJFFlow.setRemarks(ZPaiConstant.disPlay("充值"));
			if (userInfoDao.insertJF(pkJFFlow) != 1) {
				request.setAttribute("showValue", ZPaiConstant.disPlay("系统异常，请稍后重试！"));
				// 会到主页 手机验证取消
				return mapping.findForward("success");
			}
			// Use "request" to read incoming HTTP headers (e.g. cookies)
            // and HTML form data (e.g. data the user entered and submitted)

            // Use "response" to specify the HTTP response line and headers
            // (e.g. specifying the content type, setting cookies).

            ///PrintWriter out = response.getWriter();
            // Use "out" to send content to browser
            ///  out.println("Hello World");


            HttpSession session = request.getSession(true);

            /*
            '-------------------------------------------
            ' The paymentAmount is the total value of
            ' the shopping cart, that was set
            ' earlier in a session variable
            ' by the shopping cart page
            '-------------------------------------------
            */

            String paymentAmount = (String) session.getAttribute("Payment_Amount");


            /*
            '------------------------------------
            ' The returnURL is the location where buyers return to when a
            ' payment has been succesfully authorized.
            '
            ' This is set to the value entered on the Integration Assistant
            '------------------------------------
            */

            String returnURL = "www.aaa.com/Return .jsp";

            /*
            '------------------------------------
            ' The cancelURL is the location buyers are sent to when they hit the
            ' cancel button during authorization of payment during the PayPal flow
            '
            ' This is set to the value entered on the Integration Assistant
            '------------------------------------
            */
            String cancelURL = "www.aaa.com/cancel.jsp";

            /*
            '------------------------------------
            ' Calls the SetExpressCheckout API call
            '
            ' The CallShortcutExpressCheckout function is defined in the file PayPalFunctions.asp,
            ' it is included at the top of this file.
            '-------------------------------------------------
            */
            paypalfunctions ppf = new paypalfunctions();
            HashMap nvp = ppf.CallShortcutExpressCheckout (paymentAmount, returnURL, cancelURL);
            String strAck = nvp.get("ACK").toString();
            if(strAck !=null && strAck.equalsIgnoreCase("Success"))
            {
                session.setAttribute("token", nvp.get("TOKEN").toString());
                //' Redirect to paypal.com
                response.sendRedirect(response.encodeRedirectURL( nvp.get("TOKEN").toString() ));
            }
            else
            {
                // Display a user friendly Error on the page using any of the following error information returned by PayPal

                String ErrorCode = nvp.get("L_ERRORCODE0").toString();
                String ErrorShortMsg = nvp.get("L_SHORTMESSAGE0").toString();
                String ErrorLongMsg = nvp.get("L_LONGMESSAGE0").toString();
                String ErrorSeverityCode = nvp.get("L_SEVERITYCODE0").toString();
            }
				
		}
		
		request.setAttribute("showValue", ZPaiConstant.disPlay("操作成功！"));
		// 用户已经存在
		return mapping.findForward("success");
	}

}
